<?php


namespace app\common\middleware;


use Webman\Http\Request;
use Webman\Http\Response;

class RequestLog implements \Webman\MiddlewareInterface
{

    /**
     * @inheritDoc
     */
    public function process(Request $request, callable $handler): Response
    {
        return $handler($request);
        $now = date('Y-m-d H:i:s');
        echo "================================== 请求开始 {$now} ====================================", PHP_EOL;
        echo '请求路径：';
        echo $request->path(), PHP_EOL;
        echo '完整路径：';
        echo $request->fullUrl(), PHP_EOL;
        echo '请求数据：', PHP_EOL;
        echo json_encode($request->all(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), PHP_EOL;
        echo '请求头数据：', PHP_EOL;
        echo json_encode($request->header(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), PHP_EOL;
        // TODO: Implement process() method.

        $start_time = microtime(true);

        $response = $handler($request);

        $end_time = microtime(true);
        $cost_time = ($end_time - $start_time) * 1000;

        echo "响应数据：", PHP_EOL;
        echo $response->rawBody(), PHP_EOL;

        $now = date('Y-m-d H:i:s');
        echo "================================== 请求结束 {$now} 耗时：{$cost_time}MS ====================================", PHP_EOL;

        saveLog('access',$request->fullUrl(),$request->all(),[],$cost_time);

        return $response;
    }
}